/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.document.domain.BookClassification;
import com.oyun.modules.document.domain.vo.BookClassificationQueryCriteria;
import com.oyun.modules.document.mapper.BookClassificationMapper;
import com.oyun.modules.document.service.BookClassificationService;
import com.oyun.modules.medicine.domain.WordArticle;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author oyun
* @date 2024-05-10
**/
@Service
@RequiredArgsConstructor
public class BookClassificationServiceImpl extends ServiceImpl<BookClassificationMapper, BookClassification> implements BookClassificationService {

    private final BookClassificationMapper bookClassificationMapper;

    @Override
    public PageResult<BookClassification> queryAll(BookClassificationQueryCriteria criteria, Page<Object> page){
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        return PageUtil.toPage(bookClassificationMapper.findAll(criteria, page));
    }

    @Override
    public List<BookClassification> queryAll(BookClassificationQueryCriteria criteria){
        return bookClassificationMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(BookClassification resources) {
        if (resources.getParentId() == null) resources.setParentId(0l);
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BookClassification resources) {
        BookClassification bookClassification = getById(resources.getId());
        bookClassification.copy(resources);
        convertCode(bookClassification);
        saveOrUpdate(bookClassification);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<BookClassification> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BookClassification bookClassification : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("语言", bookClassification.getLanguage());
            map.put("父id", bookClassification.getParentId());
            map.put("名称(蒙)", bookClassification.getMnName());
            map.put("名称中间码", bookClassification.getMnNameCode());
            map.put("名称(汉)", bookClassification.getZhName());
            map.put(" createBy",  bookClassification.getCreateBy());
            map.put(" createTime",  bookClassification.getCreateTime());
            map.put(" updateBy",  bookClassification.getUpdateBy());
            map.put(" updateTime",  bookClassification.getUpdateTime());
            map.put(" remark",  bookClassification.getRemark());
            map.put(" status",  bookClassification.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<BookClassification> getParentType() {
        LambdaQueryWrapper<BookClassification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BookClassification::getParentId,0).eq(BookClassification::getStatus,1);
        return bookClassificationMapper.selectList(wrapper);


    }

    @Override
    public Object getByName(String name) {
        String replaceAll = IntermediateCodeUtils.mn2Code(name).replaceAll("-", " -");
        LambdaQueryWrapper<BookClassification> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(BookClassification::getZhName,name).or().like(BookClassification::getMnNameCode,replaceAll);
        return bookClassificationMapper.selectList(wrapper);
    }
    private void convertCode(BookClassification bookClassification) {
        bookClassification.setMnNameCode(IntermediateCodeUtils.mn2Code(bookClassification.getMnName()).replaceAll("-", " -"));

    }

}
